package cn.sccdlg.crawler.job.task;

/**
 * @author RaoShuang
 * @emil 991207823@qq.com
 * @date 2020/11/30 23:28
 * 获取薪水可视化转换工具类
 */
public class MathSalary {
    /**
     * 获取薪水范围
     *
     * @param salaryStr
     * @return
     */
    public static Integer[] getSalary(String salaryStr) {
        if (salaryStr == null || "".equals(salaryStr)) {
            return new Integer[]{0, 0};
        }
        // 声明存放薪水范围的数组
        Integer[] salary = new Integer[2];
        // 薪资标价 500/天，1-1.2万/月，6-9千/月，10-15万/年
        String date = salaryStr.substring(salaryStr.length() - 1, salaryStr.length());

        if (!"月".equals(date) && !"年".equals(date)) {
            salaryStr = salaryStr.substring(0, salaryStr.length() - 2);
            salary[0] = salary[1] = str2Num(salaryStr, 365);
            return salary;
        }

        String unit = salaryStr.substring(salaryStr.length() - 3, salaryStr.length() - 2);
        String[] salarys = salaryStr.substring(0, salaryStr.length() - 3).split("-");

        salary[0] = mathSalary(date, unit, salarys[0]);
        salary[1] = mathSalary(date, unit, salarys[1]);
        return salary;
    }

    private static Integer mathSalary(String date, String unit, String salaryStr) {
        Integer salary = 0;

        if ("万".equals(unit)) {
            salary = str2Num(salaryStr, 10000);
        } else {
            salary = str2Num(salaryStr, 1000);
        }

        if ("月".equals(date)) {
            salary = str2Num(salary.toString(), 12);
        }
        return salary;
    }

    /**
     * 计算薪酬数额（计算失败返回0）
     */
    private static int str2Num(String salaryStr, int num) {
        try {
            Number result = Float.parseFloat(salaryStr) * num;
            return result.intValue();
        } catch (Exception e) {
            //
        }
        return 0;
    }
}
